<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cron extends CI_Controller {
	
	/**
	 * 自动发送邮件脚本
	 */
	function emailsend(){
		$protocolMailKey = "protocolMailKey";
		$mailList = $this->mycache->get_array($protocolMailKey);
		if (count($mailList)>0){
			//echo count($mailList);
			foreach ($mailList as $key => $tmpArr){
				$this->load->model('User_model');
				$user_id=$tmpArr['user_id'];
				$borrow_id=$tmpArr['borrow_id'];
				$title = $tmpArr['borrow_title'];
				$tender_id=$tmpArr['tender_id'];
				
				//$this->load->model('User_vip_model', 'vip');
				//$this->vip->check_vip($user_id);
				
				$this->load->library('func');
				$html = $this->func->protocol_msg($borrow_id, $tender_id);
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= "借款标“{$title}”的借款协议书（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				if ($res){
					//echo "yes";
				}else{
					//echo "no";
				}
				unset($mailList[$key]);
				$this->mycache->set_array($protocolMailKey,$mailList);
				exit;
			}
		}else{
			//echo "nobody";
		}
		//echo count($mailList);
	}
	
	function yjbemailsend(){
		$yjbProtocolMailKey = "yjbProtocolMailKey";
		$mailList = $this->mycache->get_array($yjbProtocolMailKey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				$this->load->model('User_model');
				$user_id=$tmpArr['user_id'];
				$borrow_id=$tmpArr['borrow_id'];
				$tender_id=$tmpArr['tender_id'];
				
				$this->load->library('func');
				$html = $this->func->yjb_pro_msg($borrow_id,$tender_id,$user_id);
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= "易居宝（赎楼贷）协议（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				unset($mailList[$key]);
				$this->mycache->set_array($yjbProtocolMailKey,$mailList);
				exit;
			}
		}
	}

	function regmoney(){
		//$today = strtotime(date('Y-m-d'));
		//$start = $today-(3600*24*5);
		//$end = $start + 3600*24;
		$time = time()-(3600*24*5);
		$this->load->model('Mydb_model');
		$sql = "UPDATE rsh_account SET total=total-100,reg_money=reg_money-100 WHERE reg_money=100 AND user_id IN (SELECT user_id FROM rsh_user WHERE add_time<$time)";
		$this->Mydb_model->query_sql($sql, 'set');
	}
	
	function expmoney(){
		//57
		$this->load->model('Mydb_model', 'mydb');
		$sql = "select * from rsh_account where exp_money_12>0 or exp_money_24>0";
		$list = $this->mydb->query_sql($sql, 'list');
		foreach ($list as $val){
			//echo $val['user_id'],'----',$val['exp_money_12'],'----',$val['exp_money_24'],"<br>";
			$mode = substr(strval($val['user_id']),-1,1);
			$sql = "select * from rsh_account_log_{$mode} where ftype=57 order by id desc limit 1";
			$explog = $this->mydb->query_sql($sql, 'row');
			$endtime = time()-3600*24*5;//5天有效期
			if($explog['add_time']<$endtime){//到期
				$sql = "select p1.id from rsh_borrow_tender as p1 left join rsh_borrow as p2 on p1.borrow_id=p2.id 
					where p1.add_time>{$explog['add_time']} and p2.time_limit>=12 limit 1";
				$tmpRow = $this->mydb->query_sql($sql, 'row');
				if (!$tmpRow){
					//扣除
					$sql = "select * from rsh_account where user_id={$val['user_id']}";
					$account_result = $this->mydb->query_sql($sql, 'row');
					$deduct = $account_result['exp_money_12']+$account_result['exp_money_24'];
					$log['user_id'] 	= $val['user_id'];
					$log['ftype'] 		= EXP_MONEY_DEDUCT;
					$log['money'] 		= $deduct;
					$log['total'] 		= $account_result['total']-$log['money'];
					$log['use_money'] 	= $account_result['use_money']-$log['money'];
					$log['no_use_money']= $account_result['no_use_money'];
					$log['collection'] 	= $account_result['collection'];
					$log['to_user'] 	= 1;
					$log['remark'] 		= "体验金过期扣除";
					$log['add_time'] 	= time();
					$log['addip'] 		= '127.0.0.1';
					$this->mydb->add('rsh_account_log_'.$mode, $log);
					$sql = "update rsh_account set total=total-{$log['money']},use_money=use_money-{$log['money']},exp_money_12=0,exp_money_24=0 where user_id={$val['user_id']}";
					$this->mydb->query_sql($sql, 'set');
					
				}
			}
		}
		echo "over";
	}
	
	
	/**
	 * 自动发标系统
	 */
	function borrowautopublish(){
		$this->load->model('Borrow_model');
		$data_post['where']['fstatus'] = 0;
		$data_post['where']['auto_verify_time'] = strtotime(date('Y-m-d H:i',time()));
		$data_post['where']['is_check'] = 1;
		$data_post['where']['is_verify'] = 1;
		$data_post['page_size']  = 10;
		$data_post['page_start'] = 0;
		$data_post['select'] 	= 1;
		
		$vdata = $this->Borrow_model->get_borrow_user_list($data_post);
		$borrow_list = $vdata['borrow_list'];
		exit;
		if($borrow_list){
			foreach($borrow_list as $v){
				$borrow = $this->Borrow_model->get_borrow_obj($v['id']);
		  		if($borrow['fstatus'] != 0){
		  			echo "标的ID {$borrow['ID']},该标不处于待审核状态";
		  			return;
		  		}
		  		$data["fstatus"]			= 1;
		  		$data["verify_remark"]		= '审核成功';
		  		$data["verify_time"]		= $data_post['where']['auto_verify_time'];
		  		$data["verify_user"]		= 1;
				$flag = $this->Borrow_model->verify($v['id'], $data);
				if($flag == 1 && $data["fstatus"] == 1){//自动投标
					$this->Borrow_model->auto_tender_borrow($v['id']);
				}
				$index_borrow_list_key = 'rsh_index_borrow_list';
				$this->mycache->delete($index_borrow_list_key);
				$this->load->model('log_model');$this->log_model->add_log('[自动审核][发标]');
				echo $flag;
			}
		}
		echo "borrow_auto_publish over";
	}
	
	
	
	
	
	/**
	 * 手动动发标系统
	 */
	function borrowpublish($id){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Borrow_model');
		$data_post['where']['fstatus'] = 0;
		$auto_verify_time = strtotime(date('Y-m-d H:i',time()));
		$data_post['where']['is_check'] = 1;
		$data_post['where']['is_verify'] = 1;
		$data_post['where']['id'] = $id;
		$data_post['page_size']  = 10;
		$data_post['page_start'] = 0;
		$data_post['select'] 	= 1;
				
		$vdata = $this->Borrow_model->get_borrow_user_list($data_post);
		$borrow_list = $vdata['borrow_list'];
		$flag = 0;

		if($borrow_list){			
			foreach($borrow_list as $v){
				$borrow = $this->Borrow_model->get_borrow_obj($v['id']);
		  		if($borrow['fstatus'] != 0){
		  			echo $flag;
		  		}
		  		$data["fstatus"]			= 1;
		  		$data["verify_remark"]		= '审核成功';
		  		$data["verify_time"]		= $auto_verify_time;
		  		$data["verify_user"]		= 1;
				$flag = $this->Borrow_model->verify($v['id'], $data);
				
				if($flag == 1 && $data["fstatus"] == 1 && $borrow['auto_apr'] !=0 ){//自动投标
					$this->Borrow_model->auto_tender_borrow($v['id']);
				}
				$index_borrow_list_key = 'rsh_index_borrow_list';
				$this->mycache->delete($index_borrow_list_key);
				$this->load->model('log_model');
				$this->log_model->add_log('[手动审核][发标]');
			}
		}
		echo  $flag;
	}
	
	
	/**
	 * yjb自动发标系统
	 */
	
	function borrowyjbautopublish(){		
		$data_post['where']['fstatus']=1;
		$data_post['where']['auto_verify_time'] = strtotime(date('Y-m-d H:i',time()));
		$data_post['where']['is_check'] = 1;
		
		$this->load->model('Yjb_model');
		$vdata = $this->Yjb_model->get_list($data_post);
		
		$borrow_list = $vdata['list'];
		if($borrow_list){
			foreach($borrow_list as $v){
				$id = intval($v['id']);
				$setArr = array(
					'fstatus' => 2,
					'verify_user' => 1,
					'verify_time' => $data_post['where']['auto_verify_time'],
					'verify_remark' => '审核通过'
				);
				$where=array('id'=>$id);
				$res = $this->Yjb_model->update($setArr,$where);
				if ($res){
					//自动投标
					$this->Yjb_model->yjb_auto_tender($id);
				}
				$index_yjb_key = 'rsh_index_yjb';
				$this->mycache->delete($index_yjb_key);
				
				$this->load->model('log_model');
				$this->log_model->add_log('[易居宝自动审核][发标]');
				echo $res;
			}
		}		
		echo "borrow_auto_publish over";
				
	}
	
	function adlogtxt(){
		$this->load->model('log_model');		
				
		$m = array(3=>31,4=>30,5=>31,6=>30,7=>31);
		set_time_limit(0); 
		foreach ($m as $k=>$val){
			for($i = 1; $i <= $val; $i++){
				if($i<10){
					$i ='0'.$i;
				}
				$file = 'D:/workspace/newgsd/static/adlog/ad1_20140'.$k.$i.'.txt';
				if(file_exists($file)){
					$filelist_str = file_get_contents($file);
					$filelist = explode("\r", $filelist_str);
					foreach ($filelist as $v){
						$this->log_model->add_adlog(array('type'=>1,'add_time'=>strtotime('2014-0'.$k.'-'.$i.' '.$v)));
					}
					
				}
				
			}
		}
		
		/*foreach ($m as $k=>$val){
			for($i = 1; $i <= $val; $i++){
				if($i<10){
					$i ='0'.$i;
				}
				$file = 'D:/workspace/newgsd/static/adlog/ad2_20140'.$k.$i.'.txt';
				if(file_exists($file)){
					$filelist_str = file_get_contents($file);
					$filelist = explode("\r", $filelist_str);
					foreach ($filelist as $v){
						$this->log_model->add_adlog(array('type'=>2,'add_time'=>strtotime('2014-0'.$k.'-'.$i.' '.$v)));
					}
					
				}
				
			}
		}*/
		
		/*foreach ($m as $k=>$val){
			for($i = 1; $i <= $val; $i++){
				if($i<10){
					$i ='0'.$i;
				}
				$file = 'D:/workspace/newgsd/static/adlog/adreg2_20140'.$k.$i.'.txt';
				if(file_exists($file)){
					$filelist_str = file_get_contents($file);
					$filelist = explode("\r", $filelist_str);
					foreach ($filelist as $v){
						$this->log_model->add_adlog(array('type'=>3,'add_time'=>strtotime('2014-0'.$k.'-'.$i.' '.$v)));
					}
					
				}
				
			}
		}
		*/
		
		
		echo "adlog over";
		die;
	}
	
	
	function adlog(){
		$this->load->model('log_model');		
		$visit_key = 'adlog_visit_key';
		$visit_list = $this->mycache->get_array($visit_key);
		
		if($visit_list){
			foreach ($visit_list as $val){
				$data= array('ip'=>$val['ip'],'add_time'=>$val['add_time']);
				$this->log_model->add_adlog($data);
			}
			$this->mycache->delete($visit_key);
		}
		
		echo "adlog over";
		
		
	}
	
	
	function creditoremailsend(){
		$creditorMailKey = "creditorMailKey";
		$this->load->model('User_model');
		$mailList = $this->mycache->get_array($creditorMailKey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				
				$user_id=$tmpArr['user_id'];
				$creditor_id=$tmpArr['creditor_id'];
				$borrow_id=$tmpArr['borrow_id'];
				
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->load->model('Borrow_model');
				$borrow = $this->Borrow_model->get_borrow_obj($borrow_id);
				
				$this->load->model('Creditor_model');
				$clog = $this->Creditor_model->get_by_log_id($creditor_id);
			
				$this->load->library('func');
				$html = $this->func->protocol_msg_creditor($clog['obj_id'], $user_id,$creditor_id);
				
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= $borrow['title']."债权借款协议书（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				
				unset($mailList[$key]);
				$this->mycache->set_array($creditorMailKey,$mailList);
				exit;
			}
		}
		
	}
	
	function oldemailsend(){
		$oldprotocolMailKey = "oldprotocolMailKey";
		$mailList = $this->mycache->get_array($oldprotocolMailKey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				$this->load->model('User_model');
				$user_id=$tmpArr['user_id'];
				$borrow_id=$tmpArr['borrow_id'];
				$tender_id=$tmpArr['tender_id'];
				
				$this->load->model('Borrow_model');
				$borrow = $this->Borrow_model->get_borrow_obj($borrow_id);
				
				$this->load->library('func');
				$html = $this->func->protocol_msg($borrow_id, $tender_id);
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= "原借款".$borrow['title']."协议书（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				
				unset($mailList[$key]);
				$this->mycache->set_array($oldprotocolMailKey,$mailList);
				exit;
			}
		}
	}
	
	
	/**
	 * 每日回款邮件生成
	 */
	function replymail(){
		$now = time();
		$mkey = "repay_send_mail";
		$mailList = array();
		$this->load->model('Mydb_model');
		
		$sql = "SELECT p1.user_id,p2.username,p2.email,p2.privacy  
	FROM rsh_user_vip AS p1 
	LEFT JOIN rsh_user AS p2 ON p1.user_id=p2.user_id 
	WHERE p1.vip_status=1 AND p1.vip_verify_time+3600*24*365>{$now} AND p2.email_status=1 and p2.user_id=226";
		$list = $this->Mydb_model->query_sql($sql, 'list');
		foreach ($list as $val){
			$tmp = json_decode($val['privacy'],true);
			if (isset($tmp['reply']) && $tmp['reply']==1){
				$user_id = $val['user_id'];
				$start = strtotime(date('Y-m-d'));
				$end = $start+3600*24;
				$sql = "SELECT 
					SUM(t1.repay_account) AS money
					FROM rsh_borrow_collection AS t1 LEFT JOIN rsh_borrow AS t2 ON t1.borrow_id=t2.id 
					WHERE t2.fstatus=3 AND t1.user_id={$user_id} AND t1.fstatus=0 AND t1.repay_time>={$start} AND t1.repay_time<{$end};";
				$row1 = $this->Mydb_model->query_sql($sql, 'row');
			
				$sql = "SELECT SUM(repayment_account) AS money 
					FROM rsh_yjb_tender 
					WHERE user_id={$user_id} AND fstatus<3 AND repay_time>={$start} AND repay_time<{$end}";
				$row2 = $this->Mydb_model->query_sql($sql, 'row');
			
				$sql = "SELECT SUM(p1.repay_account*p4.discount_buy/p4.discount_account) AS money 
					FROM rsh_borrow_collection AS p1 
					LEFT JOIN rsh_borrow AS p2 ON p1.borrow_id=p2.id 
					LEFT JOIN rsh_borrow_tender AS p3 ON p1.tender_id=p3.id 
					LEFT JOIN rsh_creditor_log AS p4 ON p3.id=p4.tender_id 
					WHERE p2.fstatus=3 AND p4.user_id={$user_id} AND p4.fstatus<4 AND p1.fstatus=0 AND p1.repay_time>={$start} AND p1.repay_time<{$end}";
				$row3 = $this->Mydb_model->query_sql($sql, 'row');
			
				$money1 = $row1['money'] ? $row1['money'] : 0;
				$money2 = $row2['money'] ? $row2['money'] : 0;
				$money3 = $row3['money'] ? intval($row3['money']*100)/100 : 0;
				
				$money = $money1 + $money2 + $money3;
				if ($money>0){
					$msg = '亲爱的一点投用户 '.$val['username'].'，您好！您今日回款金额为：'.$money.'元，请注意查收！';
					//$mailList = $this->mycache->get_array($mkey);
					$mailList[] = array(
						'user_id'=>$user_id,
						'username'=>$val['username'],
						'email'=>$val['email'],
						'msg'=>$msg
					);
				}
				
			}
		}
		$this->mycache->set_array($mkey,$mailList);
	}
	
	/**
	 * 每日回款邮件发送
	 */
	function replymailsend(){
		$mkey = "repay_send_mail";
		$mailList = $this->mycache->get_array($mkey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$this->phpmailer->AddAddress($tmpArr['email'], $tmpArr['username']);//收件人
				$this->phpmailer->Subject 	= CON_PLATFORMNAME."回款提醒";
				$this->phpmailer->Body 		= $tmpArr['msg'];
				$this->phpmailer->Send();
				unset($mailList[$key]);
				$this->mycache->set_array($mkey,$mailList);
				exit;
			}
		}
	}
	
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cron extends CI_Controller {
    
	/**
	 * 自动发送邮件脚本
	 */
	function emailsend(){
		$protocolMailKey = "protocolMailKey";
		$mailList = $this->mycache->get_array($protocolMailKey);
		if (count($mailList)>0){
			//echo count($mailList);
			foreach ($mailList as $key => $tmpArr){
				$this->load->model('User_model');
				$user_id=$tmpArr['user_id'];
				$borrow_id=$tmpArr['borrow_id'];
				$title = $tmpArr['borrow_title'];
				$tender_id=$tmpArr['tender_id'];
				
				//$this->load->model('User_vip_model', 'vip');
				//$this->vip->check_vip($user_id);
				
				$this->load->library('func');
				$html = $this->func->protocol_msg($borrow_id, $tender_id);
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= "借款标“{$title}”的借款协议书（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				if ($res){
					//echo "yes";
				}else{
					//echo "no";
				}
				unset($mailList[$key]);
				$this->mycache->set_array($protocolMailKey,$mailList);
				exit;
			}
		}else{
			//echo "nobody";
		}
		//echo count($mailList);
	}
	
	function yjbemailsend(){
		$yjbProtocolMailKey = "yjbProtocolMailKey";
		$mailList = $this->mycache->get_array($yjbProtocolMailKey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				$this->load->model('User_model');
				$user_id=$tmpArr['user_id'];
				$borrow_id=$tmpArr['borrow_id'];
				$tender_id=$tmpArr['tender_id'];
				
				$this->load->library('func');
				$html = $this->func->yjb_pro_msg($borrow_id,$tender_id,$user_id);
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= "易居宝（赎楼贷）协议（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				unset($mailList[$key]);
				$this->mycache->set_array($yjbProtocolMailKey,$mailList);
				exit;
			}
		}
	}

	function regmoney(){
		//$today = strtotime(date('Y-m-d'));
		//$start = $today-(3600*24*5);
		//$end = $start + 3600*24;
		$time = time()-(3600*24*5);
		$this->load->model('Mydb_model');
		$sql = "UPDATE rsh_account SET total=total-100,reg_money=reg_money-100 WHERE reg_money=100 AND user_id IN (SELECT user_id FROM rsh_user WHERE add_time<$time)";
		$this->Mydb_model->query_sql($sql, 'set');
	}
	
	function expmoney(){
		//57
		$this->load->model('Mydb_model', 'mydb');
		$sql = "select * from rsh_account where exp_money_12>0 or exp_money_24>0";
		$list = $this->mydb->query_sql($sql, 'list');
		foreach ($list as $val){
			//echo $val['user_id'],'----',$val['exp_money_12'],'----',$val['exp_money_24'],"<br>";
			$mode = substr(strval($val['user_id']),-1,1);
			$sql = "select * from rsh_account_log_{$mode} where ftype=57 order by id desc limit 1";
			$explog = $this->mydb->query_sql($sql, 'row');
			$endtime = time()-3600*24*5;//5天有效期
			if($explog['add_time']<$endtime){//到期
				$sql = "select p1.id from rsh_borrow_tender as p1 left join rsh_borrow as p2 on p1.borrow_id=p2.id 
					where p1.add_time>{$explog['add_time']} and p2.time_limit>=12 limit 1";
				$tmpRow = $this->mydb->query_sql($sql, 'row');
				if (!$tmpRow){
					//扣除
					$sql = "select * from rsh_account where user_id={$val['user_id']}";
					$account_result = $this->mydb->query_sql($sql, 'row');
					$deduct = $account_result['exp_money_12']+$account_result['exp_money_24'];
					$log['user_id'] 	= $val['user_id'];
					$log['ftype'] 		= EXP_MONEY_DEDUCT;
					$log['money'] 		= $deduct;
					$log['total'] 		= $account_result['total']-$log['money'];
					$log['use_money'] 	= $account_result['use_money']-$log['money'];
					$log['no_use_money']= $account_result['no_use_money'];
					$log['collection'] 	= $account_result['collection'];
					$log['to_user'] 	= 1;
					$log['remark'] 		= "体验金过期扣除";
					$log['add_time'] 	= time();
					$log['addip'] 		= '127.0.0.1';
					$this->mydb->add('rsh_account_log_'.$mode, $log);
					$sql = "update rsh_account set total=total-{$log['money']},use_money=use_money-{$log['money']},exp_money_12=0,exp_money_24=0 where user_id={$val['user_id']}";
					$this->mydb->query_sql($sql, 'set');
					
				}
			}
		}
		echo "over";
	}
	
	
	/**
	 * 自动发标系统
	 */
	function borrowautopublish(){
		$this->load->model('Borrow_model');
		$data_post['where']['fstatus'] = 0;
		$data_post['where']['auto_verify_time'] = strtotime(date('Y-m-d H:i',time()));
		$data_post['where']['is_check'] = 1;
		$data_post['where']['is_verify'] = 1;
		$data_post['page_size']  = 10;
		$data_post['page_start'] = 0;
		$data_post['select'] 	= 1;
		
		$vdata = $this->Borrow_model->get_borrow_user_list($data_post);
		$borrow_list = $vdata['borrow_list'];
		
		if($borrow_list){
			foreach($borrow_list as $v){
				$borrow = $this->Borrow_model->get_borrow_obj($v['id']);
		  		if($borrow['fstatus'] != 0){
		  			echo "标的ID {$borrow['ID']},该标不处于待审核状态";
		  			return;
		  		}
		  		$data["fstatus"]			= 1;
		  		$data["verify_remark"]		= '审核成功';
		  		$data["verify_time"]		= $data_post['where']['auto_verify_time'];
		  		$data["verify_user"]		= 1;
				$flag = $this->Borrow_model->verify($v['id'], $data);
				if($flag == 1 && $data["fstatus"] == 1){//自动投标
					$this->Borrow_model->auto_tender_borrow($v['id']);
				}
				$index_borrow_list_key = 'rsh_index_borrow_list';
				$this->mycache->delete($index_borrow_list_key);
				$this->load->model('log_model');$this->log_model->add_log('[自动审核][发标]');
				echo $flag;
			}
		}
		echo "borrow_auto_publish over";
	}
	
	
	
	
	
	/**
	 * 手动动发标系统
	 */
	function borrowpublish($id){
		$user = $this->session->userdata('rsh_user');
		$this->load->model('Borrow_model');
		$data_post['where']['fstatus'] = 0;
		$auto_verify_time = strtotime(date('Y-m-d H:i',time()));
		$data_post['where']['is_check'] = 1;
		$data_post['where']['is_verify'] = 1;
		$data_post['where']['id'] = $id;
		$data_post['page_size']  = 10;
		$data_post['page_start'] = 0;
		$data_post['select'] 	= 1;
		
		
		
		$vdata = $this->Borrow_model->get_borrow_user_list($data_post);
		$borrow_list = $vdata['borrow_list'];
		$flag = 0;
		if($borrow_list){
			
			foreach($borrow_list as $v){
				$borrow = $this->Borrow_model->get_borrow_obj($v['id']);
		  		if($borrow['fstatus'] != 0){
		  			echo $flag;
		  		}
		  		$data["fstatus"]			= 1;
		  		$data["verify_remark"]		= '审核成功';
		  		$data["verify_time"]		= $auto_verify_time;
		  		$data["verify_user"]		= 1;
				$flag = $this->Borrow_model->verify($v['id'], $data);
				
				if($flag == 1 && $data["fstatus"] == 1 && $borrow['auto_apr'] !=0 ){//自动投标
					$this->Borrow_model->auto_tender_borrow($v['id']);
				}
				$index_borrow_list_key = 'rsh_index_borrow_list';
				$this->mycache->delete($index_borrow_list_key);
				$this->load->model('log_model');
				$this->log_model->add_log('[手动审核][发标]');
			}
		}
		echo  $flag;
	}
	
	
	/**
	 * yjb自动发标系统
	 */
	
	function borrowyjbautopublish(){		
		$data_post['where']['fstatus']=1;
		$data_post['where']['auto_verify_time'] = strtotime(date('Y-m-d H:i',time()));
		$data_post['where']['is_check'] = 1;
		
		$this->load->model('Yjb_model');
		$vdata = $this->Yjb_model->get_list($data_post);
		
		$borrow_list = $vdata['list'];
		if($borrow_list){
			foreach($borrow_list as $v){
				$id = intval($v['id']);
				$setArr = array(
					'fstatus' => 2,
					'verify_user' => 1,
					'verify_time' => $data_post['where']['auto_verify_time'],
					'verify_remark' => '审核通过'
				);
				$where=array('id'=>$id);
				$res = $this->Yjb_model->update($setArr,$where);
				if ($res){
					//自动投标
					$this->Yjb_model->yjb_auto_tender($id);
				}
				$index_yjb_key = 'rsh_index_yjb';
				$this->mycache->delete($index_yjb_key);
				
				$this->load->model('log_model');
				$this->log_model->add_log('[易居宝自动审核][发标]');
				echo $res;
			}
		}		
		echo "borrow_auto_publish over";
				
	}
	
	function adlogtxt(){
		$this->load->model('log_model');		
				
		$m = array(3=>31,4=>30,5=>31,6=>30,7=>31);
		set_time_limit(0); 
		foreach ($m as $k=>$val){
			for($i = 1; $i <= $val; $i++){
				if($i<10){
					$i ='0'.$i;
				}
				$file = 'D:/workspace/newgsd/static/adlog/ad1_20140'.$k.$i.'.txt';
				if(file_exists($file)){
					$filelist_str = file_get_contents($file);
					$filelist = explode("\r", $filelist_str);
					foreach ($filelist as $v){
						$this->log_model->add_adlog(array('type'=>1,'add_time'=>strtotime('2014-0'.$k.'-'.$i.' '.$v)));
					}
					
				}
				
			}
		}
		
		/*foreach ($m as $k=>$val){
			for($i = 1; $i <= $val; $i++){
				if($i<10){
					$i ='0'.$i;
				}
				$file = 'D:/workspace/newgsd/static/adlog/ad2_20140'.$k.$i.'.txt';
				if(file_exists($file)){
					$filelist_str = file_get_contents($file);
					$filelist = explode("\r", $filelist_str);
					foreach ($filelist as $v){
						$this->log_model->add_adlog(array('type'=>2,'add_time'=>strtotime('2014-0'.$k.'-'.$i.' '.$v)));
					}
					
				}
				
			}
		}*/
		
		/*foreach ($m as $k=>$val){
			for($i = 1; $i <= $val; $i++){
				if($i<10){
					$i ='0'.$i;
				}
				$file = 'D:/workspace/newgsd/static/adlog/adreg2_20140'.$k.$i.'.txt';
				if(file_exists($file)){
					$filelist_str = file_get_contents($file);
					$filelist = explode("\r", $filelist_str);
					foreach ($filelist as $v){
						$this->log_model->add_adlog(array('type'=>3,'add_time'=>strtotime('2014-0'.$k.'-'.$i.' '.$v)));
					}
					
				}
				
			}
		}
		*/
		
		
		echo "adlog over";
		die;
	}
	
	
	function adlog(){
		$this->load->model('log_model');		
		$visit_key = 'adlog_visit_key';
		$visit_list = $this->mycache->get_array($visit_key);
		
		if($visit_list){
			foreach ($visit_list as $val){
				$data= array('ip'=>$val['ip'],'add_time'=>$val['add_time']);
				$this->log_model->add_adlog($data);
			}
			$this->mycache->delete($visit_key);
		}
		
		echo "adlog over";
		
		
	}
	
	
	function creditoremailsend(){
		$creditorMailKey = "creditorMailKey";
		$this->load->model('User_model');
		$mailList = $this->mycache->get_array($creditorMailKey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				
				$user_id=$tmpArr['user_id'];
				$creditor_id=$tmpArr['creditor_id'];
				$borrow_id=$tmpArr['borrow_id'];
				
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->load->model('Borrow_model');
				$borrow = $this->Borrow_model->get_borrow_obj($borrow_id);
				
				$this->load->model('Creditor_model');
				$clog = $this->Creditor_model->get_by_log_id($creditor_id);
			
				$this->load->library('func');
				$html = $this->func->protocol_msg_creditor($clog['obj_id'], $user_id,$creditor_id);
				
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= $borrow['title']."债权借款协议书（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				
				unset($mailList[$key]);
				$this->mycache->set_array($creditorMailKey,$mailList);
				exit;
			}
		}
		
	}
	
	function oldemailsend(){
		$oldprotocolMailKey = "oldprotocolMailKey";
		$mailList = $this->mycache->get_array($oldprotocolMailKey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				$this->load->model('User_model');
				$user_id=$tmpArr['user_id'];
				$borrow_id=$tmpArr['borrow_id'];
				$tender_id=$tmpArr['tender_id'];
				
				$this->load->model('Borrow_model');
				$borrow = $this->Borrow_model->get_borrow_obj($borrow_id);
				
				$this->load->library('func');
				$html = $this->func->protocol_msg($borrow_id, $tender_id);
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$user_info = $this->User_model->get_by_uid($user_id);
				
				$this->phpmailer->AddAddress($user_info['email'], $user_info['username']);//收件人
				$this->phpmailer->Subject 	= "原借款".$borrow['title']."协议书（可到网站下载PDF格式）";
				$this->phpmailer->Body 		= $html;
				$res = $this->phpmailer->Send();
				
				unset($mailList[$key]);
				$this->mycache->set_array($oldprotocolMailKey,$mailList);
				exit;
			}
		}
	}
	
	
	/**
	 * 每日回款邮件生成
	 */
	function replymail(){
		$now = time();
		$mkey = "repay_send_mail";
		$mailList = array();
		$this->load->model('Mydb_model');
		
		$sql = "SELECT p1.user_id,p2.username,p2.email,p2.privacy  
	FROM rsh_user_vip AS p1 
	LEFT JOIN rsh_user AS p2 ON p1.user_id=p2.user_id 
	WHERE p1.vip_status=1 AND p1.vip_verify_time+3600*24*365>{$now} AND p2.email_status=1 and p2.user_id=226";
		$list = $this->Mydb_model->query_sql($sql, 'list');
		foreach ($list as $val){
			$tmp = json_decode($val['privacy'],true);
			if (isset($tmp['reply']) && $tmp['reply']==1){
				$user_id = $val['user_id'];
				$start = strtotime(date('Y-m-d'));
				$end = $start+3600*24;
				$sql = "SELECT 
					SUM(t1.repay_account) AS money
					FROM rsh_borrow_collection AS t1 LEFT JOIN rsh_borrow AS t2 ON t1.borrow_id=t2.id 
					WHERE t2.fstatus=3 AND t1.user_id={$user_id} AND t1.fstatus=0 AND t1.repay_time>={$start} AND t1.repay_time<{$end};";
				$row1 = $this->Mydb_model->query_sql($sql, 'row');
			
				$sql = "SELECT SUM(repayment_account) AS money 
					FROM rsh_yjb_tender 
					WHERE user_id={$user_id} AND fstatus<3 AND repay_time>={$start} AND repay_time<{$end}";
				$row2 = $this->Mydb_model->query_sql($sql, 'row');
			
				$sql = "SELECT SUM(p1.repay_account*p4.discount_buy/p4.discount_account) AS money 
					FROM rsh_borrow_collection AS p1 
					LEFT JOIN rsh_borrow AS p2 ON p1.borrow_id=p2.id 
					LEFT JOIN rsh_borrow_tender AS p3 ON p1.tender_id=p3.id 
					LEFT JOIN rsh_creditor_log AS p4 ON p3.id=p4.tender_id 
					WHERE p2.fstatus=3 AND p4.user_id={$user_id} AND p4.fstatus<4 AND p1.fstatus=0 AND p1.repay_time>={$start} AND p1.repay_time<{$end}";
				$row3 = $this->Mydb_model->query_sql($sql, 'row');
			
				$money1 = $row1['money'] ? $row1['money'] : 0;
				$money2 = $row2['money'] ? $row2['money'] : 0;
				$money3 = $row3['money'] ? intval($row3['money']*100)/100 : 0;
				
				$money = $money1 + $money2 + $money3;
				if ($money>0){
					$msg = '亲爱的一点投用户 '.$val['username'].'，您好！您今日回款金额为：'.$money.'元，请注意查收！';
					//$mailList = $this->mycache->get_array($mkey);
					$mailList[] = array(
						'user_id'=>$user_id,
						'username'=>$val['username'],
						'email'=>$val['email'],
						'msg'=>$msg
					);
				}
				
			}
		}
		$this->mycache->set_array($mkey,$mailList);
	}
	
	/**
	 * 每日回款邮件发送
	 */
	function replymailsend(){
		$mkey = "repay_send_mail";
		$mailList = $this->mycache->get_array($mkey);
		if (count($mailList)>0){
			foreach ($mailList as $key => $tmpArr){
				$this->load->library('phpmailer/PHPMailer');
				$this->phpmailer->IsSMTP();
				$this->phpmailer->AddAddress($tmpArr['email'], $tmpArr['username']);//收件人
				$this->phpmailer->Subject 	= CON_PLATFORMNAME."回款提醒";
				$this->phpmailer->Body 		= $tmpArr['msg'];
				$this->phpmailer->Send();
				unset($mailList[$key]);
				$this->mycache->set_array($mkey,$mailList);
				exit;
			}
		}
	}
	
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}